package cn.edu.nit.sys.mapper;

import cn.edu.nit.sys.entity.Course;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author 罗文斌
 * @Date 2023/3/21
 * @Version 1.0.0
 */
@Mapper
public interface CourseMapper extends BaseMapper<Course> {

    @Select("select id from sys_course where week = #{week} and day = #{day} and equip_id = #{id} and time = #{time}")
    List<String> isExist(String week, int day, int time, Long id);

    @Select("select id from sys_course where week = #{week} and day = #{day} and time = #{time} and subject_id = #{subjectId}")
    List<String> isCourse(String week, int day, int time, String subjectId);

    Page<Course> getCourseList(Page<Course> page, String siftName, String siftTime);

    @Select("select id from sys_course where subject_id = #{id}")
    List<String> isArrange(String id);

    @Select("select a.id, a.week, a.time, a.day, b.name as subject, d.name as teacher, " +
            "concat((select label from sys_dict " +
            "where value = substr(e.position from 1 for 1)), substr(e.position from 2 for 1), e.name) as position " +
            "from sys_course a " +
            "left join sys_subject b on a.subject_id = b.id " +
            "left join sys_student c on b.clazz rlike concat(c.major, c.clazz, '班') and c.grade = b.grade " +
            "left join sys_teacher d on d.id = b.teacher_id " +
            "left join sys_equip e on e.id = a.equip_id " +
            "where c.uid = #{id} and a.year = #{year} order by a.week,a.day, a.time asc")
    List<Map<String, Object>> getStudentCourse(String id, String year);

    @Select("select a.id, a.week, a.time, a.day, b.name as subject, c.name as teacher,  " +
            "concat((select label from sys_dict " +
            "where value = substr(e.position from 1 for 1)), substr(e.position from 2 for 1), e.name) as position " +
            "from sys_course a " +
            "left join sys_subject b on b.id = a.subject_id " +
            "left join sys_teacher c on b.teacher_id = c.id " +
            "left join sys_user d on d.id = c.uid " +
            "left join sys_equip e on e.id = a.equip_id where d.id = #{id} and a.year = #{year}")
    List<Map<String, Object>> getTeacherCourse(String id, String year);

    @Select("select year from sys_course GROUP BY year")
    List<String> getYearList();


    @Select("select GROUP_CONCAT(any_value(a.week)) as week, a.day, " +
            "a.time, b.name as subject, any_value(d.name) as teacher, " +
            "concat((select label from sys_dict " +
            "where value = substr(any_value(e.position) from 1 for 1)), " +
            "substr(any_value(e.position) from 2 for 1), any_value(e.name)) as position " +
            "from sys_course a " +
            "left join sys_subject b on a.subject_id = b.id " +
            "left join sys_student c on b.clazz rlike concat(c.major, c.clazz, '班') " +
            "left join sys_teacher d on d.id = b.teacher_id " +
            "left join sys_equip e on e.id = a.equip_id " +
            "where c.uid = #{uid} and a.year = #{siftTime} GROUP BY b.name, a.day, a.time")
    List<Map<String, String>> getPcList(String uid, String siftTime);

    @Select("select GROUP_CONCAT(any_value(a.week)) as week, a.day, " +
            "a.time, b.name as subject, any_value(d.name) as teacher, " +
            "concat((select label from sys_dict " +
            "where value = substr(any_value(e.position) from 1 for 1)), " +
            "substr(any_value(e.position) from 2 for 1), any_value(e.name)) as position " +
            "from sys_course a " +
            "left join sys_subject b on a.subject_id = b.id " +
            "left join sys_teacher d on d.id = b.teacher_id " +
            "left join sys_equip e on e.id = a.equip_id " +
            "where d.uid = #{id} and a.year = #{siftTime} GROUP BY b.name, a.day, a.time")
    List<Map<String, String>> getCourseForTeacher(String id, String siftTime);
}
